import { defineStore } from 'pinia'
import * as api from '@/api/dict_type'

export const dictTypeStore = defineStore('dictType', {
  state: () => ({
    dictTypeList: [],
    pagination: {
      page: 1,
      limit: 10,
      total: 0
    }
  }),

  actions: {
    async fetchDictTypePage(param) {
      const res = await api.getDictPage(param)
      this.dictTypeList = res.list || []
      this.pagination.total = res.total
    },

    async fetchDictTypeOne(id) {
      const res = await api.getDictOne(id)
      return res || {}
    },

    async addDictType(data) {
      await api.addDict(data)
    },

    async editDictType(data) {
      await api.updateDict(data)
    },

    async saveDictType(data) {
      return data.id ? 
            await this.editDictType(data) : 
            await this.addDictType(data)
    },

    async deleteDictType(id) {
      await api.deleteDict(id)
    },

    async fetchDictAll() {
      const res = await api.getDictAll()
      return res || []
    }
  }
})